package com.itheima.maplearn;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class ComparableTest2 {
    public static void main(String[] args){
        Wuguigui wuguigui1 = new Wuguigui("wuguigui1", 18);
        Wuguigui wuguigui2 = new Wuguigui("wuguigui2", 2);
        Wuguigui wuguigui3 = new Wuguigui("wuguigui3", 6);

        TreeSet<Wuguigui> testSet= new TreeSet<Wuguigui>(new wuguiguiComparator());
        testSet.add(wuguigui1);
        testSet.add(wuguigui2);
        testSet.add(wuguigui3);

        Iterator<Wuguigui> iterator = testSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }
}



class Wuguigui{
    private String name;
    private int age;

    public Wuguigui(String name,int age){
        this.name= name;
        this.age=age;
    }

    public String toString(){
        return "name:"+name+" age:"+age;
    }

    public int getAge(){
        return this.age;
    }


}
//方法二：单独在这里编写一个比较器，实现java.util.Comparator接口
class wuguiguiComparator implements Comparator<Wuguigui> {
    public int compare(Wuguigui o1,Wuguigui o2){
        return o1.getAge()-o2.getAge();

    }
}
